package com.dede.applib.util

import android.app.Activity
import java.util.*

/**
 * @author hsh
 * @date 2017/8/21 13:36.
 * @doc
 */
object ExitUtil {

    private val activitys = arrayListOf<Activity>()

    fun addActivity(activity: Activity) {
        activitys.add(activity)
    }

    fun removeActivity(activity: Activity) {
        activitys.remove(activity)
    }

    fun exit() {
        val copy = ArrayList(activitys)
        for (activity in copy) {
            activity.finish()
        }
        System.exit(0)
    }

    fun finish() {
        val copy = ArrayList(activitys)
        for (activity in copy) {
            activity.finish()
        }
    }

    fun finish(activity: Class<*>) {
        val act_: Activity? = activitys.lastOrNull { it.javaClass.name == activity.name }
        if (act_ != null) {
            activitys.remove(act_)
            act_.finish()
        }
    }
}